package nii.game.mobileserio.dialog;

import nii.game.mobileserio.action_game.R;
import nii.game.mobileserio.database.Database;
import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DialogViewScore extends Dialog implements android.view.View.OnClickListener{

	private Database db;
	private TextView[] text_diem, text_name;
	
	public DialogViewScore(Context context) 
	{
		super(context);
		
		// Nội dung hiển thị
		setContentView(R.layout.dialog_xemdiem);
		
		// Tiêu đề
		this.setTitle(Html.fromHtml("<b>TOP 10 CAO THỦ</b>"));
		
		//Database
		db = new Database(context);
		
		//TextViews
		text_diem = new TextView[10];
		text_name = new TextView[10];
		
		//=======================|| Tìm kết nối các text id ||===============================//
		// Score
		text_diem[0] = (TextView)findViewById(R.id.diem_1);
		text_diem[1] = (TextView)findViewById(R.id.diem_2);
		text_diem[2] = (TextView)findViewById(R.id.diem_3);
		text_diem[3] = (TextView)findViewById(R.id.diem_4);
		text_diem[4] = (TextView)findViewById(R.id.diem_5);
		text_diem[5] = (TextView)findViewById(R.id.diem_6);
		text_diem[6] = (TextView)findViewById(R.id.diem_7);
		text_diem[7] = (TextView)findViewById(R.id.diem_8);
		text_diem[8] = (TextView)findViewById(R.id.diem_9);
		text_diem[9] = (TextView)findViewById(R.id.diem_10);
		// Name
		text_name[0] = (TextView)findViewById(R.id.ten_1);
		text_name[1] = (TextView)findViewById(R.id.ten_2);
		text_name[2] = (TextView)findViewById(R.id.ten_3);
		text_name[3] = (TextView)findViewById(R.id.ten_4);
		text_name[4] = (TextView)findViewById(R.id.ten_5);
		text_name[5] = (TextView)findViewById(R.id.ten_6);
		text_name[6] = (TextView)findViewById(R.id.ten_7);
		text_name[7] = (TextView)findViewById(R.id.ten_8);
		text_name[8] = (TextView)findViewById(R.id.ten_9);
		text_name[9] = (TextView)findViewById(R.id.ten_10);
		
		// Cập nhật ds
		UpdateList();
		
		// Nút đóng, xóa
		Button dong = (Button)findViewById(R.id.btn_dong);
		Button xoa = (Button)findViewById(R.id.btn_xoa);
		
		// Bắt sự kiện chạm vào nút đóng, xóa
		dong.setOnClickListener(this);
		xoa.setOnClickListener(this);
	}

	//=============================================|| onClick Function ||=================================================//
	
	public void onClick(View arg0) 
	{
		switch(arg0.getId()){
		case R.id.btn_dong:
			this.dismiss();
			break;
		case R.id.btn_xoa:
			db.open();
			db.deleteAllRows();
			db.close();
			CsdlMau();
			UpdateList();
			break;
		}
	}
	
	//============================================|| Hàm update danh sách ||===============================================//
	
	public void UpdateList(){
		int i = 0;
		try {
			db.open();
			Cursor c = db.getAllRows();
			if (c.moveToFirst()) {
				do {
					String name = c.getString(c.getColumnIndex(Database.KEY_NAME));
					int math = c.getInt(c.getColumnIndex(Database.KEY_DIEM));
					text_name[i].setText(name);
					text_diem[i].setText(String.valueOf(math));
					if (i == 9)
						break;
					else
						i++;
				} while (c.moveToNext());
			}
			c.close();
			db.close();
		} catch (SQLiteException e) {
			e.printStackTrace();
		}
	}
	
	//===============================================|| Hàm Database mẫu ||================================================//
	
	public void CsdlMau(){
		// Dữ liệu mẫu gồm 10 người
		try {
			db.open();
			Cursor c = db.getAllRows();
			if (!c.moveToFirst()) {
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
				db.isertRow("Player", 0);
			}
			c.close();
			db.close();
		} 
		catch (SQLiteException e) {
			e.printStackTrace();
		}

	}

}
